﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql;
using MySql.Data;
using MySql.Data.MySqlClient;
using MySql.Data.Entity;
using ASV.Classes;
using System.Data;

namespace ASV.Admin
{
    public partial class Produkte : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            Authentication auth = new Authentication();
            int userGroup;
            int allowedGroup;

            if (auth.isLoggedIn() == true)
            {
                userGroup = int.Parse(HttpContext.Current.Session["benutzertyp"].ToString());
                allowedGroup = 2;
                if (auth.checkUserGroup(userGroup, allowedGroup) == false)
                {
                    Response.Redirect("~/Error.aspx");
                }
            }
            else
            {
                Response.Redirect("~/Error.aspx");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            MySqlCommand insertCommand;
            MySqlDataAdapter adapter;
            DataTable datatable = new DataTable();
            TableRow row;
            TableCell cellButton;
            int numrows;
            int numcells;
            Button btnEdit;
            Button btnDel;

            TableCell cell;
            Connection mycon = new Connection();
            object[] values = null;

            datatable = mycon.select("SELECT p.ID,p.Seriennummer,p.Name,p.beschreibung,p.preis,pt.Name FROM produkte as p Join produkttyp as pt on p.prodkttype = pt.ID  ORDER BY `ID` ASC");
            
            numrows = datatable.Rows.Count;
            for (int x = 0; x < numrows; x++)
            {
                values = datatable.Rows[x].ItemArray;
                numcells = datatable.Columns.Count;
                row = new TableRow();

                for (int i = 1; i < numcells; i++)
                {
                    if (i == 3)
                    {
                        TextBox test = new TextBox();

                        test.Attributes.Add("TextMode", "MultiLine");

                        test.Text = "" + values[i];

                        cell = new TableCell();
                        cell.Controls.Add(test);
                        row.Cells.Add(cell);
                    }
                    else
                    {
                        cell = new TableCell();
                        cell.Controls.Add(new LiteralControl("" + values[i]));
                        row.Cells.Add(cell);
                    }
                }
                cellButton = new TableCell();
                btnEdit = new Button();
                btnEdit.ID = "btnEdit_" + values[0];
                btnEdit.Text = "Bearbeiten";
                btnEdit.CssClass = "btn btn-primary";
                btnEdit.Click += new System.EventHandler(btnSave_Click);
                cellButton.Controls.Add(btnEdit);
                row.Cells.Add(cellButton);

                cellButton = new TableCell();
                btnDel = new Button();
                btnDel.ID = "btnVer_" + values[0];
                btnDel.Text = "Version Hinzufügen";
                btnDel.CssClass = "btn btn-primary";
                btnDel.Click += new System.EventHandler(btnSave_Click);
                cellButton.Controls.Add(btnDel);
                row.Cells.Add(cellButton);


                cellButton = new TableCell();
                btnDel = new Button();
                btnDel.ID = "btnDel_" + values[0];
                btnDel.Text = "Löschen";
                btnDel.CssClass = "btn btn-danger";
                btnDel.Click += new System.EventHandler(btnSave_Click);
                cellButton.Controls.Add(btnDel);
                row.Cells.Add(cellButton);

                table1.Rows.Add(row);
            }
        }

        public void btnSave_Click(object sender, EventArgs e)
        {
            MySqlCommand insertCommand;
            MySqlDataAdapter adapter;
            DataTable datatable = new DataTable();
            Connection mycon = new Connection();
            object[] values = null;
            Button clickedButton = (Button)sender;
            
            if (clickedButton.ID.Split('_')[0] == "btnEdit")
            {
                //BEARBEITEN
                datatable = mycon.select("SELECT `ID`, `name`,`beschreibung`,`preis` FROM `produkte` WHERE ID='" + clickedButton.ID.Split('_')[1] + "'");
                
                values = datatable.Rows[0].ItemArray;
                Response.Redirect("~/Admin/bearbeiten.aspx?ID=" + values[0].ToString() + "");
            }

            else if (clickedButton.ID == "Link2Add")
            {
                Response.Redirect("~/Admin/hinzufügen.aspx");
            }
            else
            {
                // DELETE BUTTON Produkt
                // Kann nicht gelöscht werden da Bestellungen vorhanden sein könnten

                /*insertCommand = new MySqlCommand("DELETE FROM bestellung WHERE bestellung.ID IN (SELECT bestellung.ID FROM bestellung JOIN lizenzverwaltung as lz on lz.lizenzID = bestellung.lizenzID JOIN versionsverwaltung as vv on vv.vVersionsID = lz.versionsID JOIN produkte as p on p.ID = vv.ProduktID JOIN produkttyp as pt on p.prodkttype = pt.ID WHERE p.ID ='" + clickedButton.ID.Split('_')[1] + "')", mycon.getConnection());
                insertCommand.ExecuteNonQuery();

                insertCommand = new MySqlCommand("DELETE FROM lizenzverwaltung WHERE lizenzverwaltung.lizenzID IN (SELECT lz.lizentID FROM lizenzverwaltung as lz JOIN versionsverwaltung as vv on vv.vVersionsID = lz.versionsID JOIN produkte as p on p.ID = vv.ProduktID JOIN produkttyp as pt on p.prodkttype = pt.ID WHERE p.ID ='" + clickedButton.ID.Split('_')[1] + "')", mycon.getConnection());
                insertCommand.ExecuteNonQuery();

                insertCommand = new MySqlCommand("DELETE FROM verionsverwaltung WHERE verionsverwaltung.vVersionsID IN (SELECT vv.vVersionsID FROM versionsverwaltung as vv JOIN produkte as p on p.ID = vv.ProduktID JOIN produkttyp as pt on p.prodkttype = pt.ID WHERE p.ID ='" + clickedButton.ID.Split('_')[1] + "')", mycon.getConnection());
                insertCommand.ExecuteNonQuery();

                insertCommand = new MySqlCommand("DELETE FROM `software_verwaltung`.`produkte` WHERE `produkte`.`ID` = '" + clickedButton.ID.Split('_')[1] + "'", mycon.getConnection());
                insertCommand.ExecuteNonQuery();*/

                Response.Redirect("~/Admin/Produkte.aspx");
            }
        }
    }
}